<?php
include SYSTEM_ROOTPATH . '/include/get_weather_array.php'; //采集天气
function GetWeatherShishi($WeatherCode) {
	global $db;
	global $table_top;
	$Content = file_get_contents ( "http://www.weather.com.cn/data/sk/" . $WeatherCode . ".html" );
	$rc = json_decode ( $Content, TRUE );
	//echo '<xmp>';
	//var_dump($Content);
	//echo '</xmp>';exit; 
	$time = time ();
	if (! empty ( $rc ['weatherinfo'] ['city'] )) {
		$db->update ( "UPDATE " . $table_top . "weatherinfo SET `update`='$time',`city`='" . $rc ['weatherinfo'] ['city'] . "',`temp`='" . $rc ['weatherinfo'] ['temp'] . "',`wd`='" . $rc ['weatherinfo'] ['WD'] . "',`ws`='" . $rc ['weatherinfo'] ['WS'] . "',`sd`='" . $rc ['weatherinfo'] ['SD'] . "',`ap`='" . $rc ['weatherinfo'] ['AP'] . "',`wse`='" . $rc ['weatherinfo'] ['WSE'] . "',`time`='" . $rc ['weatherinfo'] ['time'] . "',`sm`='" . $rc ['weatherinfo'] ['sm'] . "',`isradar`='" . $rc ['weatherinfo'] ['isRadar'] . "' WHERE cityid=" . $rc ['weatherinfo'] ['cityid'] );
	}
}
function get_weather_date($AreaID) {
	global $db;
	global $table_top;	
	$nowdate=date("Y-m-d");
	$Rs = $db->getone("SELECT `Update` FROM {$table_top}weatherlog WHERE AreaID='{$AreaID}' order by `id` desc");
	$Rs2 = $db->getone("SELECT `WeatherCode` FROM {$table_top}weather WHERE AreaID='{$AreaID}'");
	if ($Rs ['Update']<$nowdate) {
		return GetWeatherAll($AreaID,$Rs2['WeatherCode']);
	}
}
function GetWeather($WeatherCode) {
	global $db;
	global $table_top;
	global $tq_tu1;
	global $tq_tu2;
	$url="http://www.weather.com.cn/html/weather/$WeatherCode.shtml";
	
	//echo $url;exit;
	//$url="http://www.weather.com.cn/html/weather/101230201.shtml";
	$getarray = stream_context_create ( array ('http' => array ('method' => 'GET', 'header' => "www.weather.com.cn\r\n" . "Referer:http://www.weather.com.cn/html/weather/101020100.shtml\r\n" . "User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322)\r\n" . "Cache-Control:no-cache\r\n" ) ) );
	$Content = file_get_contents ( $url, null, $getarray );
	//$Content = file_get_contents ( $url );
	//echo $Content;exit;
	if ($Content) {
		$Sql = 'SELECT `Update`,`WeatherCode` FROM ' . $table_top . 'weather WHERE `WeatherCode`=\'' . $WeatherCode . '\'';
		$Rs = $db->getone ( $Sql );
		$up_time = $Rs ['Update'];
		if (! empty ( $up_time )) {
			unset ( $Rs );
			$up_times = "/周边地区今日天气(.*)发布/isU";
			preg_match_all ( $up_times, $Content, $up_time_array );
			$uptime = $up_time_array ['1'] ['0']; //更新时间
			$uptime = str_replace ( "(", "", $uptime );
			$uptime = str_replace ( "&nbsp;&nbsp;", " ", $uptime );
			$uptime = date ( "Y-m-d", strtotime ( $uptime ) ) . " 08:00:00";
			//if ($up_time == $uptime) {	return false;}
			//开始采集
			$search = array ("'<script[^>]*?>.*?</script>'si", "'<noscript[^>]*?>.*?</noscript>'si", "/<a(.*)>/isU", "/<\/a>/i" ); //去除js和链接
			$replace = array ("", "", "", "" );
			$Content = preg_replace ( $search, $replace, $Content );
			$Content = str_replace ( "/m/i/icon_weather/29x20/", "/images/weather/", $Content ); //替换成本地路径
			$Content = str_replace ( "/m/i/indexer/zs/", "/images/weather/", $Content );
			$Content = str_replace ( "/m2/i/icon_weather/29x20/", "/images/weather/", $Content );
			$Content = str_replace ( "/m2/i/indexer/zs/", "/images/weather/", $Content );
			unset ( $up_times, $up_time_array, $up_time, $search, $replace );
			//echo $Content;exit;
			//采集最新实况和相关数据
			$zz = "/<strong style=\"font-size:16px;color:#000;font-weight:bold;\">(.*)<\/strong>/isU";
			preg_match_all ( $zz, $Content, $rc );
			$rc = json_encode ( $rc ['1'] );
			unset ( $zz );
			//采集一周天气
			$zz = "/<td width=\"15%\" rowspan=\"2\" class=\"t0\" style=\"background:#f4f7fc;\">(.*)日(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td width=\"18%\">(.*)<\/td>.*<b>高温 <strong>(.*)℃<\/strong>.*<td width=\"15%\">(.*)<\/td>.*<td width=\"15%\">(.*)<\/td>.*<td style=\"background:#f4f7fc;\">(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td>(.*)<\/td>.*<span>低温 <strong>(.*)℃<\/strong>.*<td>(.*)<\/td>.*<td>(.*)<\/td>/isU";
			preg_match_all ( $zz, $Content, $tq );
			unset ( $zz, $tq ['0'] );
			$btwd = $tq [5] [0];
			$yjwd = $tq [11] [0];
			$qihou = GetQiHou ( trim ( $tq [4] [0] ) );
			
			//++++++++++++++++++++++++
			//生成白天天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['3'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['3'] [$i] = $tq_tu1 [$tq_tu1_id] ['img'];
				$tq ['4'] [$i] = $tq_tu1 [$tq_tu1_id] ['name'];
			}
			
			//生成夜间天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['9'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['9'] [$i] = $tq_tu2 [$tq_tu1_id] ['img'];
				$tq ['10'] [$i] = $tq_tu2 [$tq_tu1_id] ['name'];
			}
			
			//生成白天温度
			$wd1 = $tq ['5'] ['5'];
			$wda = $wd1 - 2;
			$wdb = $wd1 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['5'] [$i] = rand ( $wda, $wdb );
			}
			//生成夜间温度
			$wd2 = $tq ['11'] ['5'];
			$wda = $wd2 - 2;
			$wdb = $wd2 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['6'] [$i] = $tq ['6'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['7'] [$i] = $tq ['7'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['12'] [$i] = $tq ['12'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['13'] [$i] = $tq ['13'] ['5'];
			}
			
			//print_r ( $tq_tu1 );
			//exit ();
			$tq = json_encode ( $tq );
			$tq = str_replace ( "\\", "@", $tq );
			$tq = str_replace ( " ", "", $tq );
			//采集生活指数
			preg_match_all ( "/<div class=\"todayLiving\" id=\"index\">(.*)<\/div>/isU", $Content, $sh );
						$zz="/<dt>.*<img src=\"(.*)\" \/><\/dt>.*<b class=\".*\">(.*)<\/b>/isU";
						
			preg_match_all ( $zz, $sh ['1'] ['0'], $sh );
			unset ( $zz, $sh ['0'] );
			$sh = json_encode ( $sh );
			$sh = str_replace ( "\\", "@", $sh );
			$sh = str_replace ( " ", "", $sh );
			
			$db->update ( "UPDATE " . $table_top . "weather SET `qihou`='" . $qihou . "',`high`='" . $btwd . "',`low`='" . $yjwd . "',`Weather`='" . $tq . "',`live`='" . $sh . "',`xiangguan`='" . $rc . "',`Update`='" . $uptime . "' WHERE WeatherCode=" . $WeatherCode );
			return 1;
		}
	}
}


function GetWeatherAll($AreaID,$WeatherCode) {
	global $db;
	global $table_top;
	global $tq_tu1;
	global $tq_tu2;
	$nowdate=date("Y-m-d");
	$url="http://www.weather.com.cn/html/weather/$WeatherCode.shtml";
	$getarray = stream_context_create ( array ('http' => array ('method' => 'GET', 'header' => "www.weather.com.cn\r\n" . "Referer:http://www.weather.com.cn/html/weather/101020100.shtml\r\n" . "User-Agent:Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322)\r\n" . "Cache-Control:no-cache\r\n" ) ) );
	$Content = file_get_contents ( $url, null, $getarray );
	if ($Content) {
			//开始采集
			$search = array ("'<script[^>]*?>.*?</script>'si", "'<noscript[^>]*?>.*?</noscript>'si", "/<a(.*)>/isU", "/<\/a>/i" ); //去除js和链接
			$replace = array ("", "", "", "" );
			$Content = preg_replace ( $search, $replace, $Content );
			$Content = str_replace ( "/m/i/icon_weather/29x20/", "/images/weather/", $Content ); //替换成本地路径
			$Content = str_replace ( "/m/i/indexer/zs/", "/images/weather/", $Content );
			$Content = str_replace ( "/m2/i/icon_weather/29x20/", "/images/weather/", $Content );
			$Content = str_replace ( "/m2/i/indexer/zs/", "/images/weather/", $Content );
			unset ( $up_times, $up_time_array, $up_time, $search, $replace );
			//采集最新实况和相关数据
			$zz = "/<strong style=\"font-size:16px;color:#000;font-weight:bold;\">(.*)<\/strong>/isU";
			preg_match_all ( $zz, $Content, $rc );
			$rc = json_encode ( $rc ['1'] );
			unset ( $zz );
			//采集一周天气
			$zz = "/<td width=\"15%\" rowspan=\"2\" class=\"t0\" style=\"background:#f4f7fc;\">(.*)日(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td width=\"18%\">(.*)<\/td>.*<b>高温 <strong>(.*)℃<\/strong>.*<td width=\"15%\">(.*)<\/td>.*<td width=\"15%\">(.*)<\/td>.*<td style=\"background:#f4f7fc;\">(.*)<\/td>.*<img src=\"(.*)\" \/>.*<td>(.*)<\/td>.*<span>低温 <strong>(.*)℃<\/strong>.*<td>(.*)<\/td>.*<td>(.*)<\/td>/isU";
			preg_match_all ( $zz, $Content, $tq );
			unset ( $zz, $tq ['0'] );
			$btwd = $tq [5] [0];
			$yjwd = $tq [11] [0];
			$qihou = GetQiHou ( trim ( $tq [4] [0] ) );
			//++++++++++++++++++++++++
			//生成白天天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['3'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['3'] [$i] = $tq_tu1 [$tq_tu1_id] ['img'];
				$tq ['4'] [$i] = $tq_tu1 [$tq_tu1_id] ['name'];
			}
			
			//生成夜间天气
			for($j = 0; $j < 31; $j ++) {
				if ($tq_tu1 [$j] ['img'] == $tq ['9'] ['5']) {
					$imgid1 = $j;
					break;
				}
			}
			
			if ($imgid1 == '0') {
				$imgid1a = 0;
				$imgid1b = 2;
			} elseif ($imgid1 == '30') {
				$imgid1a == '28';
				$imgid1b == '30';
			} else {
				$imgid1a = $imgid1 - 1;
				$imgid1b = $imgid1 + 1;
			}
			
			for($i = 6; $i < 31; $i ++) {
				$tq_tu1_id = rand ( $imgid1a, $imgid1b );
				$tq ['9'] [$i] = $tq_tu2 [$tq_tu1_id] ['img'];
				$tq ['10'] [$i] = $tq_tu2 [$tq_tu1_id] ['name'];
			}
			
			//生成白天温度
			$wd1 = $tq ['5'] ['5'];
			$wda = $wd1 - 2;
			$wdb = $wd1 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['5'] [$i] = rand ( $wda, $wdb );
			}
			//生成夜间温度
			$wd2 = $tq ['11'] ['5'];
			$wda = $wd2 - 2;
			$wdb = $wd2 + 2;
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['11'] [$i] = rand ( $wda, $wdb );
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['6'] [$i] = $tq ['6'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['7'] [$i] = $tq ['7'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['12'] [$i] = $tq ['12'] ['5'];
			}
			for($i = 6; $i < 31; $i ++) {
				$tq ['13'] [$i] = $tq ['13'] ['5'];
			}
			//print_r($tq);exit;
			//++++++++++++++++++++++++++++++
			$tq = json_encode ( $tq );
			$tq = str_replace ( "\\", "@", $tq );
			$tq = str_replace ( " ", "", $tq );
			//采集生活指数
			preg_match_all ( "/<div class=\"todayLiving\" id=\"index\">(.*)<\/div>/isU", $Content, $sh );
	$zz="/<dt>.*<img src=\"(.*)\" \/><\/dt>.*<b class=\".*\">(.*)<\/b>/isU";
			preg_match_all ( $zz, $sh ['1'] ['0'], $sh );
			unset ( $zz, $sh ['0'] );
			//print_r($sh);exit;
			$sh = json_encode ( $sh );
			$sh = str_replace ( "\\", "@", $sh );
			//$sh = str_replace ( " ", "", $sh );
			$db->update("UPDATE {$table_top}weatherlog SET `qihou`='{$qihou}',`high`='{$btwd}',`low`='{$yjwd}',`Weather`='{$tq}',`live`='{$sh}',`xiangguan`='{$rc}' WHERE `AreaID`='{$AreaID}' and `Update`='{$nowdate}'");
		
	}
}

function GetQiHou($qihou) {
	if (empty ( $qihou )) {
		return false;
	}
	if (strstr($qihou,'晴')) {
		$qihouid = '1';
	} elseif (strstr($qihou,'阴')) {
		$qihouid = '2';
	} elseif (strstr($qihou,'雨')) {
		$qihouid = '3';
	} elseif (strstr($qihou,'雪')) {
		$qihouid = '4';
	} elseif ($qihou == '多云') {
		$qihouid = '5';
	}
	return $qihouid;
}

?>
